package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Override
    public void add(List<OrderSetting> list) {
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                long result = orderSettingDao.findByOrderDate(orderSetting.getOrderDate());
                if(result>0){
                    //找到了，则修改日期
                    orderSettingDao.update(orderSetting);
                }else{
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String begin=date+"-1";   //2019-9-1
        String end=date+"-31";   //2019-9-31
        Map<String,String> map=new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        List<OrderSetting> list=orderSettingDao.getOrderSettingByMonth(map);
        List<Map> result=new ArrayList<>();
        if(list!=null&&list.size()>0){
            for (OrderSetting orderSetting : list) {
                Map<String,Object> m=new HashMap<>();
                m.put("date",orderSetting.getOrderDate().getDate());   //获取日期（几号）
                m.put("number",orderSetting.getNumber());
                m.put("reservations",orderSetting.getReservations());
                result.add(m);
            }
        }
        return result;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        long number = orderSettingDao.findByOrderDate(orderDate);
        if(number>0){ //已经进行了预约设置
            //修改
            orderSettingDao.update(orderSetting);
        }else{
            //没有预约设置
            //添加
            orderSettingDao.add(orderSetting);
        }
    }
}